package com.qiqu.service.impl;

import com.qiqu.entity.Comments;
import com.qiqu.entity.Enclosure;
import com.qiqu.dao.EnclosureDao;
import com.qiqu.service.EnclosureService;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("enclosureService")
public class EnclosureServiceImpl implements EnclosureService {
    @Resource
    private EnclosureDao enclosureDao;
    @Resource
    private Sid sid;

    @Override
    public boolean insert(Enclosure enclosure) {
        enclosure.setId(sid.nextShort());
        enclosure.setCreateTime(new Date());
        return enclosureDao.insert(enclosure)>0;
    }

    @Override
    public boolean deleteByArticle(String articleId) {
        Example e = new Example(Enclosure.class);
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("article",articleId);
        return enclosureDao.deleteByExample(e)>0;
    }

    @Override
    public List<Enclosure> queryByArticle(String articleId) {
        Example e = new Example(Enclosure.class);
        Example.Criteria criteria = e.createCriteria();
        criteria.andEqualTo("article",articleId);
        return enclosureDao.selectByExample(e);
    }

    @Override
    public boolean addEnclosure(Enclosure enclosure) {
        enclosure.setId(sid.nextShort());
        return enclosureDao.insert(enclosure)>0;
    }


}